package com.qianfeng.smartmetting.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianfeng.smartmetting.entity.Booking;
import com.qianfeng.smartmetting.entity.TimeSlot;
import com.qianfeng.smartmetting.entity.dto.BookingDTO;
import com.qianfeng.smartmetting.entity.vo.MyBooking;
import com.qianfeng.smartmetting.mapper.BookingMapper;
import com.qianfeng.smartmetting.service.BookingService;
import com.qianfeng.smartmetting.service.TimeSlotService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class BookingServiceImpl extends ServiceImpl<BookingMapper, Booking>
        implements BookingService {

    @Autowired
    private TimeSlotService timeSlotService;

    @Autowired
    private BookingMapper bookingMapper;

    @Override
    @Transactional //开启事务注解
    public boolean svaeBooking(BookingDTO bookingDTO) {

        //需要先添加时间记录timeslot表，然后在进行添加预约记录
        TimeSlot timeSlot = new TimeSlot();
        BeanUtils.copyProperties(bookingDTO,timeSlot);
//        timeSlot.setTimeQujian(bookingDTO.getTimeQujian());
//        timeSlot.setStartTime(bookingDTO.getStartTime());
//        timeSlot.setEndTime(bookingDTO.getEndTime());
//        timeSlot.setUseYear(bookingDTO.getUseYear());
//        timeSlot.setUseMonth(bookingDTO.getUseMonth());
//        timeSlot.setUseDay(bookingDTO.getUseDay());
//        timeSlot.setTimeLong(bookingDTO.getTimeLong());
        boolean timeFlag = timeSlotService.save(timeSlot);
        Booking booking = new Booking();
        BeanUtils.copyProperties(bookingDTO,booking);
//        booking.setRoomId(bookingDTO.getRoomId());
//        booking.setState(bookingDTO.getState());
//        booking.setUserId(bookingDTO.getUserId());
        booking.setTimeId(timeSlot.getTimeId());
        int result = bookingMapper.insert(booking);
        return result>0?true:false;
    }

    @Override
    public List<MyBooking> getMyBooking(Integer userId) {
        return bookingMapper.getMyBooking(userId);
    }

    @Override
    public Integer isBooking(Integer roomid, String starttime, String endtime) {
        return bookingMapper.isBooking(roomid,starttime,endtime);
    }
}
